package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WarningReceiver;
import com.ruoyi.system.service.IWarningReceiverService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警接收人Controller
 * 
 * @author ruoyi
 * @date 2021-06-20
 */
@RestController
@RequestMapping("/system/receiver")
public class WarningReceiverController extends BaseController
{
    @Autowired
    private IWarningReceiverService warningReceiverService;

    /**
     * 查询预警接收人列表
     */
    @PreAuthorize("@ss.hasPermi('system:receiver:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarningReceiver warningReceiver)
    {
        startPage();
//        warningReceiver.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
//        warningReceiver.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        warningReceiver.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<WarningReceiver> list = warningReceiverService.selectWarningReceiverList(warningReceiver);
        return getDataTable(list);
    }

    /**
     * 导出预警接收人列表
     */
    @PreAuthorize("@ss.hasPermi('system:receiver:export')")
    @Log(title = "预警接收人", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WarningReceiver warningReceiver)
    {
        warningReceiver.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        warningReceiver.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        warningReceiver.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<WarningReceiver> list = warningReceiverService.selectWarningReceiverList(warningReceiver);
        ExcelUtil<WarningReceiver> util = new ExcelUtil<WarningReceiver>(WarningReceiver.class);
        return util.exportExcel(list, "预警接收人数据");
    }

    /**
     * 获取预警接收人详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receiver:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(warningReceiverService.selectWarningReceiverById(id));
    }

    /**
     * 新增预警接收人
     */
    @PreAuthorize("@ss.hasPermi('system:receiver:add')")
    @Log(title = "预警接收人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarningReceiver warningReceiver)
    {
//        选取用户的id
        warningReceiver.setUserId(warningReceiver.getUserId());
//        发布用户的DeptId,TenantCode
        warningReceiver.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        warningReceiver.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        return toAjax(warningReceiverService.insertWarningReceiver(warningReceiver));
    }

    /**
     * 修改预警接收人
     */
    @PreAuthorize("@ss.hasPermi('system:receiver:edit')")
    @Log(title = "预警接收人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarningReceiver warningReceiver)
    {
        return toAjax(warningReceiverService.updateWarningReceiver(warningReceiver));
    }

    /**
     * 删除预警接收人
     */
    @PreAuthorize("@ss.hasPermi('system:receiver:remove')")
    @Log(title = "预警接收人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(warningReceiverService.deleteWarningReceiverByIds(ids));
    }
}
